{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Copy of Detailed Expression Capture and Animation.ipynb",
      "private_outputs": true,
      "provenance": [],
      "collapsed_sections": [],
      "toc_visible": true,
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/YadiraF/DECA/blob/master/Detailed_Expression_Capture_and_Animation.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "<center>\n",
        "\n",
        "# DECA: Detailed Expression Capture and Animation\n",
        "\n",
        "Code in [![GitHub stars](https://img.shields.io/github/stars/yadiraf/DECA?style=social)](https://github.com/YadiraF/DECA)\n",
        "\n",
        "Page at [![](https://img.shields.io/badge/Project-Page-blue?style=flat&logo=Google%20chrome&logoColor=blue)](https://deca.is.tue.mpg.de/)\n",
        "\n",
        "Made by [![Yao](https://img.shields.io/twitter/follow/yaofeng1995?style=social)](https://twitter.com/yaofeng1995)\n",
        "\n",
        "![extreme](https://deca.is.tue.mpg.de/uploads/ckeditor/pictures/642/content_teaser.gif)\n",
        "\n",
        "</center>\n",
        "\n",
        "Thanks [mhoangvslev](https://github.com/mhoangvslev) for contributing to this Colab document. "
      ],
      "metadata": {
        "id": "LKXziS2IzAg2"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Setup"
      ],
      "metadata": {
        "id": "j0hyU8s5f_SB"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "%tensorflow_version 1.x"
      ],
      "metadata": {
        "id": "x8Gm4LXA3Fay"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "bB5Ii6bkBYWc"
      },
      "outputs": [],
      "source": [
        "#@title Setup dependencies\n",
        "\n",
        "%cd /content/\n",
        "!git clone https://github.com/yadiraf/DECA\n",
        "\n",
        "%cd DECA/\n",
        "!apt -q install -y zip unzip ffmpeg libsm6 libxext6\n",
        "#!pip install -r requirements.txt\n",
        "!pip install 'torch==1.6.0'\n",
        "!pip install 'torchvision==0.7.0'\n",
        "!pip install -q 'pytorch3d==0.2.5'\n",
        "!pip install -q numpy scipy chumpy scikit-image opencv-python PyYAML face-alignment yacs kornia ninja fvcore\n",
        "!pip install -q lucid>=0.2.3 gdown matplotlib\n",
        "#!pip install --upgrade ipykernel"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "#@title Download models\n",
        "#@markdown By executing this cell, you agree to the [LICENSE](https://flame.is.tue.mpg.de/modellicense.html) provided by Max-Planck-Gesellschaft zur Förderung der Wissenschaften e.V\n",
        "\n",
        "print(\"Downloading FLAME2020 model...\")\n",
        "!gdown --id 18eHkbq2W3aJJVUNTM3QwFK0dPaeNoRAz -O FLAME2020.zip\n",
        "!unzip -o FLAME2020.zip -d data/\n",
        "\n",
        "print(\"Downloading deca_model...\")\n",
        "!gdown --id 1rp8kdyLPvErw2dTmqtjISRVvQLj6Yzje -O data/deca_model.tar"
      ],
      "metadata": {
        "id": "ZmSRqqrvCIwx"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Face reconstruction"
      ],
      "metadata": {
        "id": "TuLFayzVf6h-"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#@title Run paper demo\n",
        "print(\"Setting up...\")\n",
        "!pip install -q kornia==0.4.0 yacs==0.1.8 face_alignment ninja fvcore\n",
        "\n",
        "print(\"Check for NVIDIA Driver...\")\n",
        "!nvidia-smi\n",
        "\n",
        "print(\"Running experiments...\")\n",
        "import os\n",
        "input_folder = \"TestSamples/AFLW2000\" #@param {type:\"string\"}\n",
        "output_folder = os.path.join(input_folder, \"results\")\n",
        "!python demos/demo_reconstruct.py -i $input_folder -s $output_folder --saveDepth True --saveObj True\n"
      ],
      "metadata": {
        "id": "iiFP_JPZHjVf"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#@title Use your own image\n",
        "#@markdown Upload your images to `upload` folder under `DECA`\n",
        "print(\"Setting up...\")\n",
        "!pip install -q kornia yacs face_alignment ninja fvcore\n",
        "\n",
        "print(\"Check for NVIDIA Driver...\")\n",
        "!nvidia-smi\n",
        "\n",
        "print(\"Running experiments...\")\n",
        "import os\n",
        "input_folder = \"/content/\" #@param {type:\"string\"}\n",
        "output_folder = os.path.join(input_folder, \"results\")\n",
        "!python demos/demo_reconstruct.py -i $input_folder -s $output_folder --saveDepth True --saveObj True\n",
        "\n",
        "## show results \n",
        "print('visualize one exmaple below')\n",
        "import matplotlib.pyplot as plt\n",
        "from glob import glob\n",
        "from PIL import Image\n",
        "vispath_list = glob(output_folder+'/*_size.jpg')\n",
        "for vispath in vispath_list:\n",
        "  image = Image.open(vispath)\n",
        "  plt.figure(figsize=(20, 20))\n",
        "  plt.imshow(image)\n",
        "  plt.axis(\"off\");\n",
        "  break\n",
        "print(f'Please check all results in {output_folder}')\n"
      ],
      "metadata": {
        "id": "Ny9lRkxefRPz"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "zuBCgeH08tdn"
      },
      "source": [
        "#@title Download the result\n",
        "import os\n",
        "from google.colab import files\n",
        "\n",
        "print(next(os.walk(output_folder)))\n",
        "folders = [ os.path.join(output_folder, f) for f in next(os.walk(os.path.join(input_folder, 'results')))[1] ]\n",
        "\n",
        "print(f'Download results...')\n",
        "os.system(f'zip -r DECA_results.zip {\" \".join(folders)}')\n",
        "files.download(\"DECA_results.zip\")"
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}